package com.jn.algorithm.day009.question27;

/**
 * @author 江南大学1033190417
 * @date 2022/4/24 23:03
 */
public class Solution {
    //模拟
    public int removeElement(int[] nums, int val) {
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[count++] = nums[i];
            }
        }
        return count;
    }

    //双指针
    public int removeElement2(int[] nums, int val) {
        int fast = 0, low = 0;
        int length = nums.length;
        while (fast < length) {
            if (nums[fast] != val) {
                nums[low++] = nums[fast];
            }
            ++fast;
        }
        return low;
    }

    //双指针2 2 2 2 3 3
    public int removeElement3(int[] nums, int val) {
        int left = 0, right = nums.length - 1;
        while (left < right) {
            if (nums[left] == val) {
                nums[left] = nums[right--];
            } else {
                ++left;
            }
        }
        return left;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
    }
}
